package com.meixiang.view.wheel.time;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.tool.Tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间选择Dialog
 * author：wangzuzhen on 2016/4/5 0005 15:13.
 */
public class TimeSelectDialog extends Dialog {

    public interface TimeSelectListener {
        public void getTimeSelect(String date, String time);
    }

    private Context context;
    private String title;
    private String leftTitle;
    private String rightTitle;
    private String data;
    private View view;
    private TimeSelectListener timeSelectListener;
    private TextView tv_left_title = null;

    private TextView tv_right_title = null;
    private TextView mTitleTextView = null;
    private View mDecorView = null;

    private boolean scrolling = false;
    private TextView mViewDate = null;
    private WheelView mViewHour = null;
    private WheelView mViewMinute = null;

    private NumericWheelAdapter hourAdapter;
    private NumericWheelAdapter minuteAdapter;

    public TimeSelectDialog(Context context, String data, String title, String leftTitle, String rightTitle, TimeSelectListener timeSelectListener) {
        super(context);
        this.context = context;
        this.timeSelectListener = timeSelectListener;
        this.title = title;
        this.leftTitle = leftTitle;
        this.rightTitle = rightTitle;
        this.data = data;
    }

    public OnWheelChangedListener listener = new OnWheelChangedListener() {

        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (!scrolling) {
                updateTimes(mViewHour, mViewMinute);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_time_select, null);
        setContentView(view);
        super.onCreate(savedInstanceState);
        initDialog();
        mDecorView = getWindow().getDecorView();

        mViewDate = (TextView) findViewById(R.id.wheel1);
        mViewHour = (WheelView) findViewById(R.id.wheel2);
        mViewMinute = (WheelView) findViewById(R.id.wheel3);

        tv_left_title = (TextView) findViewById(R.id.tv_left_title);
        tv_right_title = (TextView) findViewById(R.id.tv_right_title);
        mTitleTextView = (TextView) findViewById(R.id.title);
        mTitleTextView.setText(title);
        tv_left_title.setText(leftTitle);
        tv_right_title.setText(rightTitle);
        tv_left_title.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {//取消
                // TODO Auto-generated method stub
                dismiss();
            }
        });
        tv_right_title.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {//完成
                // TODO Auto-generated method stub
                if (null != hourAdapter.getValues() && null != minuteAdapter.getValues()) {
                    timeSelectListener.getTimeSelect(mViewDate.getText().toString(), hourAdapter.getValues() + ":" + minuteAdapter.getValues());
                    dismiss();
                }
            }
        });

        mViewHour.addChangingListener(listener);
        mViewMinute.addChangingListener(listener);
        updateTimes(mViewHour, mViewMinute);

        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);//获取当前的小时数/24小时制
        int minute = calendar.get(Calendar.MINUTE);//获取当前的分钟数
        mViewHour.setCurrentItem(hour);//显示当前的小时数
        mViewMinute.setCurrentItem(minute);//显示当前的分钟数
        mViewDate.setText(data);//显示当前的日期

    }

    private void initDialog() {
        setCancelable(false);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        int width = Tool.getWindowWith(context);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    /**
     * 显示时间
     */
    private void updateTimes(WheelView timeHour, WheelView timeMinute) {
        hourAdapter = new NumericWheelAdapter(0, 23, "%02d");
        timeHour.setAdapter(hourAdapter);
        timeHour.setCyclic(true);
        timeHour.setVisibleItems(5);
        minuteAdapter = new NumericWheelAdapter(0, 59, "%02d");
        timeMinute.setAdapter(minuteAdapter);
        timeMinute.setCyclic(true);
        timeMinute.setVisibleItems(5);
    }

    private String getNowTime(Calendar calendar) {
        String time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
        time = sdf.format(new Date(calendar.getTimeInMillis()));
        return time;

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }


}
